package com.ll.app.controller;

import com.ll.app.api.req.ListSignupReqDTO;
import com.ll.app.api.req.SignupApplyReqDTO;
import com.ll.app.api.req.SignupReqDTO;
import com.ll.base.dto.ApiResultDTO;
import com.ll.base.enums.QRCodeTypeEnum;
import com.ll.base.util.QRCodeUtils;
import com.ll.domain.classbase.biz.SignupDomainService;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

@Slf4j
@RestController
@RequestMapping("/signup")
@RequiredArgsConstructor
@Api(tags = "报名相关API")
public class SignupController {

    private final SignupDomainService signupDomainService;

    @ResponseBody
    @ApiOperation("申请报名")
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public ApiResultDTO apply(@RequestBody @Validated SignupApplyReqDTO reqDTO){
        return signupDomainService.apply(reqDTO);
    }
    @ResponseBody
    @ApiOperation("取消报名")
    @RequestMapping(value = "/cancelApply", method = RequestMethod.POST)
    public ApiResultDTO cancelApply(@RequestBody @Validated SignupApplyReqDTO reqDTO){
        return signupDomainService.cancelApply(reqDTO);
    }

    @ResponseBody
    @ApiOperation("生成签到码图片（学员端发起请求）")
    @RequestMapping(value = "/createSignupERPic", method = RequestMethod.GET)
    public void createSignupERPic(@RequestParam("studentUserId") String studentUserId,
                                  @RequestParam("courseId") Long courseId,
                                  HttpServletResponse response) throws IOException {
        // 设置响应流信息
        response.setContentType("image/jpg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        OutputStream stream = response.getOutputStream();

        //type是1，生成活动详情、报名的二维码，type是2，生成活动签到的二维码
        String content = QRCodeTypeEnum.签到二维码.ordinal() + "|" +studentUserId + "|" + courseId;
        //获取一个二维码图片
        BitMatrix bitMatrix = QRCodeUtils.createCode(content);
        //以流的形式输出到前端
        MatrixToImageWriter.writeToStream(bitMatrix , "jpg" , stream);
    }


    @ResponseBody
    @ApiOperation("签到（教练端发起请求）")
    @RequestMapping(value = "/signup", method = RequestMethod.POST)
    public ApiResultDTO signup(@RequestBody @Validated SignupReqDTO reqDTO){
        reqDTO.checkSignup();
        return signupDomainService.signup(reqDTO);
    }

    @ResponseBody
    @ApiOperation("获取课程报名列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResultDTO queryListByCourseId(@RequestBody @Validated ListSignupReqDTO reqDTO){
        return signupDomainService.queryListByCourseId(reqDTO);
    }
}


